package com.medicalsystem.controller;

import com.medicalsystem.entity.Diagnosis;
import com.medicalsystem.service.DiagnosisService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/diagnoses")
@Tag(name = "诊断记录管理", description = "提供诊断记录的增删改查功能")
public class DiagnosisController {

    private final DiagnosisService diagnosisService;

    public DiagnosisController(DiagnosisService diagnosisService) {
        this.diagnosisService = diagnosisService;
    }

    @Operation(summary = "获取所有诊断记录", description = "返回所有诊断记录列表")
    @GetMapping
    public List<Diagnosis> getAllDiagnoses() {
        return diagnosisService.getAllDiagnoses();
    }

    @Operation(summary = "根据ID获取诊断记录", description = "通过诊断ID获取诊断记录详细信息")
    @GetMapping("/{id}")
    public ResponseEntity<Diagnosis> getDiagnosisById(@PathVariable Long id) {
        Diagnosis diagnosis = diagnosisService.getDiagnosisById(id);
        return diagnosis != null ? ResponseEntity.ok(diagnosis) : ResponseEntity.notFound().build();
    }

    @Operation(summary = "新增诊断记录", description = "保存诊断记录到数据库")
    @PostMapping
    public Diagnosis saveDiagnosis(@RequestBody Diagnosis diagnosis) {
        return diagnosisService.saveDiagnosis(diagnosis);
    }

    @Operation(summary = "修改诊断记录", description = "根据ID修改诊断记录详细信息")
    @PutMapping("/{id}")
    public ResponseEntity<Diagnosis> updateDiagnosis(@PathVariable Long id, @RequestBody Diagnosis diagnosis) {
        Diagnosis updatedDiagnosis = diagnosisService.updateDiagnosis(id, diagnosis);
        return updatedDiagnosis != null ? ResponseEntity.ok(updatedDiagnosis) : ResponseEntity.notFound().build();
    }

    @Operation(summary = "删除诊断记录", description = "根据ID删除诊断记录")
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteDiagnosis(@PathVariable Long id) {
        diagnosisService.deleteDiagnosis(id);
        return ResponseEntity.noContent().build();
    }
}
